<template>
  <div class="app-container">
    <el-form ref="objectForm" :model="object" label-width="150px">
      <title-container describe="广告设置" />
      <div class="gpo-form mt-20">
        <el-form-item label="模块名称：" prop="name">
          <el-input v-model="name" disabled/>
        </el-form-item>
        <el-form-item label="应用终端：" prop="applicationTerminal">
          <el-select v-model="applicationTerminal" placeholder="所属范围" clearable>
            <el-option v-for="(item, index) in GLOBAL.scopeTypes" :key = "index" :label= "item" :value="item"/>
          </el-select>
        </el-form-item>
        <el-form-item label="图片：" prop="thumb">
          <single-image
            :images="fileList"
            :time="false"
            @success="setImage"/>
        </el-form-item>
      </div>
    </el-form>
    <div class="gpo-btn text-center" >
      <el-button type="primary" class="gpo-btn_small" @click="onBack">返回</el-button>
      <el-button type="primary" class="gpo-btn_small" @click="onSubmit(message)">提交</el-button>
    </div>
  </div>
</template>
<script>
import TitleContainer from '@/components/GpoCommon/TitleContainer'
import SingleImage from '@/components/Upload/singleImage'
export default {
  name: 'CreateNews',
  components: { TitleContainer, SingleImage },
  data() {
    return {
      message: '新增广告',
      fileList: [],
      informationTypes: [], // 分类
      buildingTypes: [], // 楼盘
      applicationTerminal: '',
      name: '',
      object: {
        'bannerlist': [],
        'name': '',
        'status': 0,
        'applicationTerminal': '',
        'parentId': this.$route.params.id
      }
    }
  },
  created() {
    // 获取基本信息
    const query2 = {
      id: this.$route.params.id,
      plateType: 'gnmpp_fangyuns'
    }
    this.searchListAll(query2).then((res) => {
      this.name = res.data.list ? res.data.list[0].object.name : ''
      this.applicationTerminal = res.data.list ? res.data.list[0].object.applicationTerminal : ''
      // 初始化楼盘
      let query1 = {
        parentId: this.$route.params.id,
        pageSize: 10,
        pageNum: 1
      }
      query1 = this.setListQuery(query1)
      this.searchListAll(query1).then((res) => {
        if (res.data.list.length > 0) {
          this.object = res.data.list[0]
          this.applicationTerminal = res.data.list[0].object.applicationTerminal
          this.fileList = this._.map(res.data.list[0].object.bannerlist, function(v) {
            return { name: '', imgUrl: v.url }
          })
          console.log(this.fileList)
        }
      })
    })
  },
  methods: {
    // 上传多图
    setImage(file) {
      const _this = this
      if (file.length === 0) {
        if (this.object.id) {
          this.object.object.bannerlist = []
        } else {
          this.object.bannerlist = []
        }
        return
      }
      this._.forEach(file, function(f) {
        if (_this.object.id) {
          _this.object.object.bannerlist.push(
            {
              'url': f.imgUrl,
              'serialNumber': ''
            }
          )
        } else {
          _this.object.bannerlist.push(
            {
              'url': f.imgUrl,
              'serialNumber': ''
            }
          )
        }
      })
    },
    //
    onSubmit() {
      let objectData = {}
      if (!this.object.id) {
        this.object.applicationTerminal = this.applicationTerminal
        objectData = {
          object: this.object,
          plateType: 'gnmpp_fangyuns'
        }
      } else {
        this.object.object.applicationTerminal = this.applicationTerminal
        objectData = this.object
      }
      console.log(objectData)
      // return;
      this.saveNewMedia(objectData, '新增推荐好房')
    }
  }
}
</script>
